

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
	console.log(request); // 发送的消息
    console.log(sender); // 发送者的信息，tabs相关信息
    sendResponse(sender);// 这里将tab信息发送给发送者
});


var backgd = {
	tabId:null,
	init:function(){
		if(chrome.tabs.getCurrent){
			chrome.tabs.query({active: true, lastFocusedWindow: true},function (tab) {
				var url = tab[0].url;
				backgd.tabId = tab[0].id;
			});
		}else if(chrome.tabs.getSelected){
			chrome.tabs.getSelected(null, function (tab) {
				var url = tab.url;
				backgd.tabId = tab.id;
			});
		}else if(chrome.tabs.query){
			chrome.tabs.query({active: true, lastFocusedWindow: true}, 
				
				tabs => {
			    var url = tabs[0].url;
			    // use `url` here inside the callback because it's asynchronous!
			});
		}
		
	},
	notice:function(type,args){
		function toCopy() {
		    copyOpt.copy();
		}
		function showNextBtn() {
//		    nexthtml.showNextBtn();
		}
		function next(i) {
//		     nexthtml.toNext(i);
		}
		if(type == 1){
			chrome.scripting.executeScript({
			    target: { tabId: backgd.tabId },
			    function: toCopy,
			});
		}else if(type == 2){
			chrome.scripting.executeScript({
			    target: { tabId: backgd.tabId },
			    function: removeNextBtn,
			});
		}else if(type == 3){
			chrome.scripting.executeScript({
			    target: { tabId: backgd.tabId },
			    function:next,
			    args:args
			});
		}
	},
	// 创建右键菜单
	creatMenu:function(){
		const  opt = {
			type: 'normal',
		    visible: true,
		    title: '复制csnd',
		    id: '125',
		    contexts: ['all']
		}
		chrome.contextMenus.create(opt, function(info,tab){
			 function toCopy(args){
            	copyOpt.copy();
            }
	    });
	    chrome.contextMenus.onClicked.addListener(function(info,tab){
	        backgd.tabId = tab.id;
            function toCopy(args){
            	copyOpt.copy();
            }
			var args = [1];
			chrome.scripting.executeScript({
			    target: { tabId: tab.id },
			    function:toCopy,
			    args:args
			});
	    });
	}
}
backgd.creatMenu();
